package com.internetCafes.spms.common.AIRecognize.handler.impl;

import com.internetCafes.spms.common.AIRecognize.handler.WordHandler;
import com.internetCafes.spms.web.certificate.vo.AIRecognizeInfo;
import org.springframework.stereotype.Component;

/**
 * @author LinYe
 */
@Component
public class ISOWordHandler implements WordHandler {

    @Override
    public AIRecognizeInfo handle(String word, String filePath) {
        AIRecognizeInfo aiRecognizeInfo = new AIRecognizeInfo();
        String name = handleName(word);
        if (name!=null){
            aiRecognizeInfo.setName(name);
        }
        String code = handleCode(word);
        if (code!=null){
            aiRecognizeInfo.setCode(code);
        }
        String authOrgan = handleAuthOrgan(word);
        if (authOrgan!=null){
            aiRecognizeInfo.setAuthOrgan(authOrgan);
        }
        String authStandards = handleAuthStandards(word);
        if (authStandards!=null){
            aiRecognizeInfo.setAuthStandards(authStandards);
        }

        return aiRecognizeInfo;
    }

    public String handleName(String word) {
        String[] split = word.split("\n");
        for (String line : split) {
            if (line.contains("认证证书")) {
                return line;
            }
        }
        return null;
    }

    public String handleCode(String word) {
        String[] split = word.split("\n");
        for (String s : split) {
            String line = s;
            if (line.contains("号")) {
                int prefix = line.indexOf(":");
                if (prefix == -1) {
                    prefix = line.indexOf("：");
                }
                if (prefix != -1) {
                    line = line.substring(prefix + 1);
                }
                return line.replace(" ", "");
            }
        }
        return null;
    }

    public String handleAuthOrgan(String word) {
        String[] split = word.split("\n");
        for (String s : split) {
            String line = s;
            if (line.contains("认证中心") || line.contains("认证有限公司")) {
                int prefix = line.indexOf(":");
                if (prefix == -1) {
                    prefix = line.indexOf("：");
                }
                if (prefix != -1) {
                    line = line.substring(prefix + 1);
                }
                return line;
            }
        }
        return null;
    }

    public String handleAuthStandards(String word) {
        String[] split = word.split("\n");
        StringBuilder result= new StringBuilder();
        for (String line : split) {
            if (line.contains("GB/T") || line.contains("ISO") || line.contains("1SO")) {
                result.append(line+" ");
            }
        }
        return result.toString();
    }
}
